
package analisadorsintatico;

import java.util.ArrayList;

/**
 *
 * @author Anderson
 */
public class GraphNode {

    public enum Tipo {
        NORMAL,
        IF,
        WHILE,
        TRUE,
        FALSE,
        DO,
        START,
        END
    }

    private String command;
    private Tipo tipo;
    private ArrayList<GraphNode> nodes;

    public GraphNode() {
        command = "";
        nodes = new ArrayList<GraphNode>();
    }

    public GraphNode(Tipo tipo) {
        command = "";
        this.tipo = tipo;
        nodes = new ArrayList<GraphNode>();
    }

    public GraphNode(String command, Tipo tipo) {
        this.command = command;
        this.tipo = tipo;
        nodes = new ArrayList<GraphNode>();
    }

    public void updateCommand(String text) {
        command += text;
    }

    public void setTipo(Tipo tipo) {
        this.tipo = tipo;
    }

    public void addNode(GraphNode node) {
        nodes.add(node);
    }

    public ArrayList<GraphNode> getNodes() {
        return nodes;
    }

    public int getDepth() {
        int depth = 0;
        int tempDepth;
//        System.out.println("qntd nodes = " + nodes.size());
        for(GraphNode node : nodes) {
            tempDepth = node.getDepth();
//            System.out.println("oi = " + tempDepth);
            if(depth < tempDepth) {
                depth = tempDepth;
            }
        }
        if(tipo != Tipo.TRUE && tipo != Tipo.FALSE && tipo != Tipo.DO) {
            depth++;
        }
        
        return depth;
    }

    public Tipo getTipo() {
        return tipo;
    }

    public String getCommand() {
        return command;
    }

    public int getProgramDepth() {
        int programDepth = 0;
//        System.out.println("qntd nodes = " + nodes.size());
        for(GraphNode node : nodes) {
//            System.out.println("node: " + node.toString() + " = " + node.getDepth());
            programDepth += node.getProgramDepth();
        }
        if(tipo != Tipo.TRUE && tipo != Tipo.FALSE && tipo != Tipo.DO) {
            programDepth++;
        }

        return programDepth;
    }

    public int getBreadth() {
        int breadth = 0;
        if(tipo == Tipo.IF || tipo == Tipo.DO) {
            breadth++;
        }
        for(GraphNode node : nodes) {
            breadth += node.getBreadth();
        }
//        System.out.println(breadth);
        return breadth;
    }

    public void display() {
        System.out.print('{');
        System.out.print(command);
        System.out.print(", ");
        System.out.print(tipo);
        System.out.print(", ");
        System.out.print(nodes.size());
        System.out.println("}");
    }

    public void displayNode() {
        System.out.print('{');
        System.out.print(command);
        System.out.print(", ");
        System.out.print(tipo);
        System.out.println("}");
        System.out.println("-----");
        for(GraphNode node : nodes) {
            System.out.print(command + " " + tipo + " : ");
            node.displayNode();
        }
    }

    public void displayLeaves() {
        if(nodes.isEmpty()) {
            System.out.print('{');
            System.out.print(command);
            System.out.print(", ");
            System.out.print(tipo);
            System.out.println("}");
            System.out.println("-----");
        }
        for(GraphNode node : nodes) {
//            System.out.print(command + " " + tipo + " : ");
            node.displayLeaves();
        }
    }

    @Override
    public String toString() {
        String node = "";
        node += '{' + command + ", " + tipo + "}";
        return node;
    }
}
